package model;

import android.util.Log;
import com.baidu.mapapi.model.LatLng;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by mashuai on 14-11-27.
 */
//lat=39.98437&long=116.30987&access_token=2.00IRQisFRVvxKB34dbc9c972rxmLwD
public class LocationDataRequester {

    //位置信息获取
    final static String ACCESS_TOKEN = "2.00IRQisFRVvxKB34dbc9c972rxmLwD";

    public List<Map<String,Object>> request(double lat,double lon,String q,int count,int range) throws IOException, JSONException {
        /**参数说明
         lat 纬度
         lon 经度
         q 关键字
         count 单页返回的记录条数
         range 查询半径
         */
        HashMap<String,Object> map = null;



        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("lat",lat+""));
        params.add(new BasicNameValuePair("long",lon+""));
        params.add(new BasicNameValuePair("q",q));
        params.add(new BasicNameValuePair("range",range+""));
        params.add(new BasicNameValuePair("count",count+""));
        params.add(new BasicNameValuePair("access_token",ACCESS_TOKEN));

        String paramStr = URLEncodedUtils.format(params, "utf-8");
        Log.d("paramStr",paramStr);

        HttpGet request = new HttpGet("https://api.weibo.com/2/place/nearby/pois.json?"+paramStr);

        DefaultHttpClient httpClient = new DefaultHttpClient();


        HttpResponse response = httpClient.execute(request);
        HttpEntity httpEntity = response.getEntity();
        InputStream inputStream = httpEntity.getContent();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len=(inputStream.read(buffer))) != -1) {
            outputStream.write(buffer,0,len);
        }

        String result = outputStream.toString();
        Log.d("result", result);

        JSONObject resultJSONObject = new JSONObject(result);

        JSONArray poisJSONArray = resultJSONObject.getJSONArray("pois");

        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        String category_name = null;
        String title = null;
        String phone = null;
        String poi_street_address = null;
        String distance = null;
        String poiMap = null;
        LatLng mLatLon = null;

        for (int i=0;i<poisJSONArray.length();i++) {
            JSONObject jsonObject = poisJSONArray.getJSONObject(i);
            category_name = (String) jsonObject.get("category_name");//分类
            title = jsonObject.optString("title");//餐馆名称
            phone = jsonObject.optString("phone");//电话
            poi_street_address = jsonObject.optString("poi_street_address");//地址
            distance = jsonObject.optString("distance");//距离
            poiMap = jsonObject.optString("map");
            model.LocationManager locationManager = new model.LocationManager();
            double gLat = Double.parseDouble(poiMap.substring(poiMap.indexOf("=")+1,poiMap.indexOf(",")));
            double gLon = Double.parseDouble(poiMap.substring(poiMap.indexOf(",")+1,poiMap.indexOf("&zoom")));
            mLatLon = locationManager.coordinateConversion(gLat, gLon);

            map = new HashMap<String,Object>();
            map.put("category_name",category_name);
            map.put("title",title);
            map.put("phone",phone);
            map.put("poi_street_address", poi_street_address);
            map.put("distance", distance);
            map.put("mLatLon",mLatLon);
            map.put("gLat",gLat);
            map.put("gLon",gLon);

            list.add(map);
        }
        return list;
    }



}